import logging
import time

from selenium.webdriver import Keys

from Page.UserPage.ErpLoginPage import ErpLoginPage
from selenium.webdriver.common.by import By as by



class ExceptionProcess(ErpLoginPage):
    """表示层"""

    # 错误会员卡号
    vip_number_useless = "654321"

    # 正确会员卡号
    vip_number_used = "12314"

    # 错误商品信息
    goods_useless = "小米手环10"

    # 正确商品信息
    goods_used = "小米手环9"

    # 零售管理
    retail_management = (by.XPATH, '(//div[@class="ant-menu-submenu-title"])[1]')

    # 零售出库
    retail_delivery = (by.XPATH, '//a[@target="零售出库"]')

    # 新增按钮
    new_order = (by.CSS_SELECTOR, '.table-operator .ant-btn-primary')

    # 点击引导关闭
    skip_button = (by.CSS_SELECTOR, '.introjs-skipbutton')

    # 会员卡号点击
    click_vip = (by.CSS_SELECTOR, '#organId')

    # 会员卡号输入框
    input_vip = (by.CSS_SELECTOR, '#organId .ant-select-search__field')

    # 会员号错误信息文本
    vip_number_text = (by.XPATH,'//*[text()="暂无数据"]')

    # 商品输入框
    input_goods = (by.XPATH,'//*[text()="输入条码或名称"]')

    # 商品错误信息文本
    goods_text = (by.XPATH,'//*[text()="需在商品管理先新增才能使用"]')

    # 选择商品
    select_goods = (by.XPATH, '//span[@class="ant-input-group ant-input-group-compact"]/button')

    # 商品名称输入小米手环9,外面记得按回车
    xiaomi_band = (by.CSS_SELECTOR, '[placeholder="条码、名称、规格、型号、颜色"]')

    # 选择小米手环9
    select_xiaomi_band = (by.CSS_SELECTOR, 'tbody.ant-table-tbody:nth-child(3)>tr.ant-table-row:nth-of-type(1)')

    # 查看刚刚的库存，只获取了文本
    data_num = (by.CSS_SELECTOR, 'tbody.ant-table-tbody:nth-child(3)>tr:nth-of-type(1)>td:nth-of-type(10)')

    # 确定按钮
    determine = (by.XPATH, '(//button[@class="ant-btn ant-btn-primary"])[6]')

    # 保存并审核
    save_check = (by.CSS_SELECTOR, 'div.ant-modal-footer>button:nth-of-type(3)')




    """操作层"""

    def input_wrong_information(self):
        """打开新增并输入错的信息"""
        # 进入零售管理
        self.click(self.retail_management)
        time.sleep(1)
        logging.info("进入零售管理")

        # 进入零售出库
        self.click(self.retail_delivery)

        # 点击新增
        self.click(self.new_order)

        # 跳过引导
        self.click(self.skip_button)

        # 点击会员卡输入框
        self.click(self.click_vip)

        # 输入错误会员卡号
        self.send_keys(self.input_vip, self.vip_number_useless)
        time.sleep(1)

        # 获取提示文本
        vip_text = self.get_element_text(self.vip_number_text)

        # 按tab键
        self.send_keys_tab(self.input_vip)
        time.sleep(1)

        # 点击商品输入框
        self.click(self.input_goods)
        time.sleep(1)


        # 输入错误商品名字
        # self.send_keys(self.input_goods,self.goods_useless)

        # 获取提示文本
        goods_text = self.get_element_text(self.goods_text)
        time.sleep(1)



        # 断言看看是否一致
        assert vip_text == "暂无数据"
        assert goods_text == "需在商品管理先新增才能使用"
        logging.info("提升文本断言通过")


    def input_right_information(self):
        """输入正确的信息并且审核"""
        # 点击会员卡输入框
        self.click(self.click_vip)

        # 输入正确会员卡号
        self.send_keys(self.input_vip, self.vip_number_used)

        # 用js点击商品搜索
        self.js_click(self.select_goods)
        time.sleep(1)

        # 输入小米手环9并回车
        self.send_keys(self.xiaomi_band,self.goods_used)
        self.send_keys_enter(self.xiaomi_band)
        time.sleep(1)

        # 选择小米手环9
        self.click(self.select_xiaomi_band)

        # 获取出库前的库存
        text = self.get_element_text(self.data_num)
        num = int(text)

        # 点击确定
        self.click(self.determine)

        # 点击保存并审核
        self.click(self.save_check)

        # 返回库存量
        return num

    def get_inventory(self):

        # 点击新增
        self.click(self.new_order)

        # 用js点击商品搜索
        self.js_click(self.select_goods)

        # 输入小米手环9并回车
        self.send_keys(self.xiaomi_band, self.goods_used)
        self.send_keys_enter(self.xiaomi_band)
        # 不加时间读不出来
        time.sleep(1)

        # 获取出库前的库存
        text = self.get_element_text(self.data_num)
        num = int(text)

        return num

if __name__ == '__main__':
    ep = ExceptionProcess()

    ep.common_login()
    ep.input_wrong_information()
    num1 =ep.input_right_information()
    num2 = ep.get_inventory()
    print(num1,num2)
    assert num1-num2==1
    logging.info("断言成功，库存正确")





